﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO = Microsoft.MSE.Catalog.Contracts;
using System.Data;
using System.Data.SqlClient;

namespace Microsoft.MSE.Catalog.Module.Data
{
    public class MSESystem
    {
        public MSESystem()
        {

        }

        public static Guid CreateSystem(DTO.System system, SqlTransaction txn, SqlConnection conn)
        {
            Guid ID;
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[CreateSystem]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                    SqlParameter NameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 400);
                    SqlParameter OwnerNameParam = new SqlParameter("@OwnerName", SqlDbType.NText);
                    SqlParameter OwnerContactParam = new SqlParameter("@OwnerContact", SqlDbType.NChar, 10);
                    SqlParameter LocationParam = new SqlParameter("@Location", SqlDbType.NText);
                    SqlParameter URIParam = new SqlParameter("@URI", SqlDbType.NVarChar, 500);
                    SqlParameter DescriptionParam = new SqlParameter("@Description", SqlDbType.NText);
                    SqlParameter PolicyIDParam = new SqlParameter("@PolicyID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Direction = ParameterDirection.Output;
                    NameParam.Value = system.Name;
                    if (system.OwnerName != null)
                    {
                        OwnerNameParam.Value = system.OwnerName;
                    }
                    else
                    {
                        OwnerNameParam.Value = DBNull.Value;
                    }
                    if (system.OwnerContact != null)
                    {
                        OwnerContactParam.Value = system.OwnerContact;
                    }
                    else
                    {
                        OwnerContactParam.Value = DBNull.Value;
                    }
                    if (system.Description != null)
                    {
                        DescriptionParam.Value = system.Description;
                    }
                    else
                    {
                        DescriptionParam.Value = DBNull.Value;
                    }
                    LocationParam.Value = system.Location;
                    if (system.Policy != null)
                    {
                        PolicyIDParam.Value = system.Policy.ID;
                    }
                    else
                    {
                        PolicyIDParam.Value = DBNull.Value;
                    }
                    if (system.URI != null)
                    {
                        URIParam.Value = system.URI;
                    }
                    else
                    {
                        URIParam.Value = DBNull.Value;
                    }

                    cmd.Parameters.Add(IDParam);
                    cmd.Parameters.Add(NameParam);
                    cmd.Parameters.Add(OwnerNameParam);
                    cmd.Parameters.Add(OwnerContactParam);
                    cmd.Parameters.Add(LocationParam);
                    cmd.Parameters.Add(URIParam);
                    cmd.Parameters.Add(DescriptionParam);
                    cmd.Parameters.Add(PolicyIDParam);

                    cmd.ExecuteNonQuery();
                    ID = (Guid)IDParam.Value;
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return ID;
        }

        public static DTO.System GetSystem(Guid ID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            DTO.System system = new DTO.System();
            const string sprocname = "[ServiceEngine].[GetSystem]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = ID;
                    cmd.Parameters.Add(IDParam);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            reader.Read();
                            system.ID = reader.GetGuid(0);
                            system.Name = reader.GetString(1);
                            if (reader.IsDBNull(2))
                            {
                                system.OwnerName = null;
                            }
                            else
                            {
                                system.OwnerName = reader.GetString(2);
                            }
                            if (reader.IsDBNull(3))
                            {
                                system.Location = null;
                            }
                            else
                            {
                                system.Location = reader.GetString(3);
                            }

                            if (reader.IsDBNull(4))
                            {
                                system.Description = null;
                            }
                            else
                            {
                                system.Description = reader.GetString(4);
                            }
                            if (reader.IsDBNull(5))
                            {
                                system.OwnerContact = null;
                            }
                            else
                            {
                                system.OwnerContact = reader.GetString(5);
                            }
                            system.Policy = reader.IsDBNull(6) ? null : CatalogSvcHelper.AssemblePolicyFromPolicyAssertion(reader.GetGuid(6), txn, conn);
                            if (reader.IsDBNull(7))
                            {
                                system.URI = null;
                            }
                            else
                            {
                                system.URI = reader.GetString(7);
                            }
                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return system;

        }

        public static void DeleteSystem(Guid ID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[DeleteSystem]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = ID;
                    cmd.Parameters.Add(IDParam);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

        }

        public static DTO.System UpdateSystem(DTO.System system, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[UpdateSystem]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                    SqlParameter NameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 400);
                    SqlParameter OwnerNameParam = new SqlParameter("@OwnerName", SqlDbType.NText);
                    SqlParameter OwnerContactParam = new SqlParameter("@OwnerContact", SqlDbType.NChar, 10);
                    SqlParameter LocationParam = new SqlParameter("@Location", SqlDbType.NText);
                    SqlParameter URIParam = new SqlParameter("@URI", SqlDbType.NVarChar, 500);
                    SqlParameter DescriptionParam = new SqlParameter("@Description", SqlDbType.NText);
                    SqlParameter PolicyIDParam = new SqlParameter("@PolicyID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = system.ID;
                    NameParam.Value = system.Name;
                    if (system.OwnerName != null)
                    {
                        OwnerNameParam.Value = system.OwnerName;
                    }
                    else
                    {
                        OwnerNameParam.Value = DBNull.Value;
                    }
                    if (system.OwnerContact != null)
                    {
                        OwnerContactParam.Value = system.OwnerContact;
                    }
                    else
                    {
                        OwnerContactParam.Value = DBNull.Value;
                    }
                    if (system.Description != null)
                    {
                        DescriptionParam.Value = system.Description;
                    }
                    else
                    {
                        DescriptionParam.Value = DBNull.Value;
                    }
                    if (system.Location != null)
                    {
                        LocationParam.Value = system.Location;
                    }
                    else
                    {
                        LocationParam.Value = DBNull.Value;
                    }
                    if (system.Policy != null)
                    {
                        PolicyIDParam.Value = system.Policy.ID;
                    }
                    else
                    {
                        PolicyIDParam.Value = DBNull.Value;
                    }
                    if (system.URI != null)
                    {
                        URIParam.Value = system.URI;
                    }
                    else
                    {
                        URIParam.Value = DBNull.Value;
                    }

                    cmd.Parameters.Add(IDParam);
                    cmd.Parameters.Add(NameParam);
                    cmd.Parameters.Add(OwnerNameParam);
                    cmd.Parameters.Add(OwnerContactParam);
                    cmd.Parameters.Add(LocationParam);
                    cmd.Parameters.Add(URIParam);
                    cmd.Parameters.Add(DescriptionParam);
                    cmd.Parameters.Add(PolicyIDParam);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return system;
        }

        public static DTO.SystemList ListSystems(SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            DTO.SystemList list = new DTO.SystemList();
            const string sprocname = "[ServiceEngine].[ListSystems]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            list.Systems = new List<DTO.SystemListElement>();
                            while (reader.Read())
                            {
                                DTO.SystemListElement element = new DTO.SystemListElement();
                                element.ID = reader.GetGuid(0);
                                element.Name = reader.GetString(1);
                                if (reader.IsDBNull(2))
                                {
                                    element.Description = null;
                                }
                                else
                                {
                                    element.Description = reader.GetString(2);
                                }
                                list.Systems.Add(element);
                            }
                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return list;

        }

        public static List<DTO.SystemListElement> ListSystemsByGroup(Guid entityGroupID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            List<DTO.SystemListElement> list = new List<DTO.SystemListElement>();
            const string sprocname = "[ServiceEngine].[ListEntitiesByGroup]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@entityGroupID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = entityGroupID;
                    cmd.Parameters.Add(IDParam);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            list = new List<DTO.SystemListElement>();
                            while (reader.Read())
                            {
                                DTO.SystemListElement element = new DTO.SystemListElement();
                                element.ID = reader.GetGuid(0);
                                element.Name = reader.GetString(1);
                                if (reader.IsDBNull(2))
                                {
                                    element.Description = null;
                                }
                                else
                                {
                                    element.Description = reader.GetString(2);
                                }

                                list.Add(element);
                            }
                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return list;

        }
        
    }

}
